<?php

/**
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */

if ($_POST) {
    require_once(dirname(__FILE__).'/../config/config.inc.php');
    
    ob_start();
    
    $location = __BASE_URI__.'pesquisa';
    
    if (!empty($_POST['type'])) {
        $type = DbGet::types($_POST['type']);
        $location .= '/tipo.'.urlencode(strtolower($type->name));
        if (!empty($_POST['brand'])) {
            $brand = DbGet::brands($type->id, $_POST['brand']);
            $location .= '/marca.'.urlencode(strtolower($brand->name));
            if (!empty($_POST['model'])) {
                $model = DbGet::models($type->id, $brand->id, $_POST['model']);
                $location .= '/modelo.'.urlencode(strtolower($model->name));
            }
        }
    }
    if (!empty($_POST['initial-year']))
        $location .= '/ano-inicial.'.$_POST['initial-year'];
    if (!empty($_POST['final-year']))
        $location .= '/ano-final.'.$_POST['final-year'];
    if (!empty($_POST['initial-price']))
        $location .= '/preco-inicial.'.$_POST['initial-price'];
    if (!empty($_POST['final-price']))
        $location .= '/preco-final.'.$_POST['final-price'];
    
    header('Location: '.$location);
}